using System;
using System.Web.UI.WebControls;
using Iesi.Collections;
using Iesi.Collections.Generic;
using Blazing.WebFacade;
using MenuItem=Blazing.BusinessObjects.Cms.MenuItem;
using Blazing.BusinessObjects.Cms;
using System.Collections.Generic;

namespace Blazing.UI.Cms.Admin.Usercontrols
{
    public partial class EditPageMenus : System.Web.UI.UserControl
    {
        private ISet<Blazing.BusinessObjects.Cms.MenuItem> viewStateItems
        {
            get
            {
                if (ViewState["viewStateItems"] == null)
                    return new HashedSet<Blazing.BusinessObjects.Cms.MenuItem>();
                return ViewState["viewStateItems"] as ISet<Blazing.BusinessObjects.Cms.MenuItem>;
            }
            set
            {
                ViewState["viewStateItems"] = value;
            }
        }
        
        public ISet<Blazing.BusinessObjects.Cms.MenuItem> MenuItems
        {
            get
            {
                UpdateRemovedItems();
                return viewStateItems;
            }
            set
            {
                viewStateItems = value;
                RepeaterMenuItems.DataSource = value;
                RepeaterMenuItems.DataBind();
            }
        }

        protected List<int> ActiveMenuIDs
        {
            get
            {
                List<int> activeIds = new List<int>();
                foreach (RepeaterItem item in RepeaterMenuItems.Items)
                {
                    Usercontrols_SelectMenuItem control = item.FindControl("SelectMenuItem1") as Usercontrols_SelectMenuItem;
                    if (control != null && control.MenuItem != null && control.IsActive)
                    {
                        activeIds.Add(control.MenuItem.ID);
                    }
                }
                return activeIds;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void LinkButtonAddBinding_Click(object sender, EventArgs e)
        {
            UpdateRemovedItems();

            ISet<Blazing.BusinessObjects.Cms.MenuItem> items = viewStateItems;

            if (items == null)
            {
                items = new HashedSet<Blazing.BusinessObjects.Cms.MenuItem>();
            }
            if (AddNewMenuItemSelector.MenuItem != null)
                items.Add(AddNewMenuItemSelector.MenuItem);

            RepeaterMenuItems.DataSource = items;
            RepeaterMenuItems.DataBind();

            AddNewMenuItemSelector.Reset();
            AddNewMenuItemSelector.ExcludeMenuItems = ActiveMenuIDs;
            AddNewMenuItemSelector.UpdateExcludedItems();
        }

        private void UpdateRemovedItems()
        {
            List<int> activeIds = ActiveMenuIDs;

            List<MenuItem> removeItems = new List<MenuItem>();
            foreach(MenuItem checkItem in viewStateItems)
            {
                if(!activeIds.Contains(checkItem.ID))
                    removeItems.Add(checkItem);
            }

            foreach (MenuItem item in removeItems)
            {
                viewStateItems.Remove(item);
            }
        }
    }
}